import java.util.ArrayList;
import java.util.*;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
     int s = 0;
     ArrayList<TreeNode> list = new ArrayList<>();
     ArrayList<Integer> listv;
     ArrayList<ArrayList<Integer>> listAll = new ArrayList<>();

    public void adjust(){
        if(list.size()<2)
            return;
        TreeNode end = list.get(list.size()-1);
        TreeNode temp;
        Iterator<TreeNode> iterator = list.listIterator();
        while(true){
            temp = list.get(list.size()-2);
            if(temp.left!=end && temp.right!=end){
                s = s-temp.val;
                list.remove(list.size()-2);
            }else{
                return;
            }
        }
    }


    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target){
        if(root!=null){
            list.add(root);
            s = s+root.val;
        }else{
            return listAll;
        }
        if(root.left==null && root.right==null){//leaf
            adjust();
            if(s==target){
                
                listv = new ArrayList<>();
                for (TreeNode t:list) {
                    listv.add(t.val);
                }
                listAll.add(listv);
            }
            list.remove(list.size()-1);
            s = s-root.val;
        }else{//see list element, not root child, delete middle element
            adjust();
        }
        if(root.left!=null)
            FindPath(root.left,target);
        if(root.right!=null)
            FindPath(root.right,target);
        return listAll;
    }
    
   
}